package com.xdcplus.web.controller.alarm;


import cn.hutool.core.lang.Assert;
import com.xdcplus.exceptions.domain.ResponseVO;
import com.xdcplus.mom.controller.AbstractController;
import com.xdcplus.mom.enums.ResponseEnum;
import com.xdcplus.pager.vo.PageVO;
import com.xdcplus.web.common.pojo.dto.alarm.ToolAlarmCodeDTO;
import com.xdcplus.web.common.pojo.dto.alarm.ToolAlarmCodeFilterDTO;
import com.xdcplus.web.common.pojo.dto.alarm.ToolAlarmCodeUpdateDTO;
import com.xdcplus.web.common.pojo.vo.alarm.ToolAlarmCodeVO;
import com.xdcplus.web.service.alarm.ToolAlarmCodeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.Validation;
import javax.validation.constraints.NotNull;
import java.io.IOException;


/**
 * 设备告警码表(ToolAlarmCode)表服务控制层
 *
 * @author bullion.yan
 * @since 2024-06-05 16:16:15
 */
@Api(tags = "设备告警码表(ToolAlarmCode)") 
@Slf4j
@Validated
@RestController
@AllArgsConstructor
@RequestMapping("toolAlarmCode")
public class ToolAlarmCodeController extends AbstractController {

    @Autowired
    private ToolAlarmCodeService toolAlarmCodeService;

    @ApiOperation("新增设备告警码表")
    @PostMapping(value = "", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseVO saveToolAlarmCode(@Valid  @RequestBody ToolAlarmCodeDTO toolAlarmCodeDTO) {
        Validation.buildDefaultValidatorFactory().getValidator().validate(toolAlarmCodeDTO);
        toolAlarmCodeDTO.setCreatedUser(getAccount());
        toolAlarmCodeService.saveToolAlarmCode(toolAlarmCodeDTO);
        return ResponseVO.success();
    }

    @ApiOperation("修改设备告警码表")
    @PutMapping(value = "", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseVO updateToolAlarmCode(@RequestBody  @Valid ToolAlarmCodeUpdateDTO toolAlarmCodeUpdateDTO) {
        toolAlarmCodeUpdateDTO.setUpdatedUser(getAccount());
        toolAlarmCodeService.updateToolAlarmCode(toolAlarmCodeUpdateDTO);

        return ResponseVO.success();
    }

    @ApiOperation("删除设备告警码表")
    @DeleteMapping(value = "/{toolAlarmCodeId}", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "toolAlarmCodeId", dataTypeClass = Long.class, value = "设备告警码表ID", required = true),
    })
    public ResponseVO deleteToolAlarmCodeLogical(@PathVariable("toolAlarmCodeId")
                                         @NotNull(message = "设备告警码表ID不能为空") Long toolAlarmCodeId) {
        toolAlarmCodeService.deleteToolAlarmCodeLogical(toolAlarmCodeId);
        return ResponseVO.success();
    }
    
    @ApiOperation("批量删除设备告警码表")
    @DeleteMapping(value = "/deleteByIds/{toolAlarmCodeIds}", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "toolAlarmCodeId", dataTypeClass = Long.class, value = "设备告警码表ID", required = true),
    })
    public ResponseVO deleteToolAlarmCodeLogical(@PathVariable("toolAlarmCodeIds")
                                         @NotNull(message = "设备告警码表ID不能为空") String toolAlarmCodeIds) {
        toolAlarmCodeService.deleteToolAlarmCodeByIds(toolAlarmCodeIds);
        return ResponseVO.success();
    }
    @ApiOperation("查询设备告警码表")
    @GetMapping(value = "", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseVO<PageVO<ToolAlarmCodeVO>> findToolAlarmCode(ToolAlarmCodeFilterDTO toolAlarmCodeFilterDTO) {
        Validation.buildDefaultValidatorFactory().getValidator().validate(toolAlarmCodeFilterDTO);
        return ResponseVO.success(toolAlarmCodeService.queryToolAlarmCode(toolAlarmCodeFilterDTO));
    }
    @ApiOperation("导入")
    @PutMapping(value = "importAlarmCode/{groupIdFk}", produces = MediaType.MULTIPART_FORM_DATA_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "groupIdFk", dataTypeClass = Long.class, value = "告警组ID", required = true),
    })

    public  ResponseVO importAlarmCode(MultipartFile file,
                                                @PathVariable("groupIdFk")
                                                @NotNull(message = "告警组ID不能为空") Long groupIdFk, HttpServletResponse response) throws IOException {
        Assert.isTrue(file!=null, ResponseEnum.REQUEST_PARAMETER_IS_ERROR.getMessage());
        Assert.isTrue(groupIdFk!=null, ResponseEnum.REQUEST_PARAMETER_IS_ERROR.getMessage());
        toolAlarmCodeService.importAlarmCode(file,groupIdFk,response,getAccount());
        return ResponseVO.success();
    }
    @ApiOperation("导出")
    @GetMapping(value = "exportAlarmCode/{groupIdFk}", produces = MediaType.MULTIPART_FORM_DATA_VALUE)
    public  ResponseVO exportAlarmCode(@PathVariable("groupIdFk")
                                                @NotNull(message = "告警组ID不能为空") Long groupIdFk, HttpServletResponse response) throws IOException {
        Assert.isTrue(groupIdFk!=null, ResponseEnum.REQUEST_PARAMETER_IS_ERROR.getMessage());
        toolAlarmCodeService.exportAlarmCode(groupIdFk,response,getAccount());
        return ResponseVO.success();
    }

}
